package com.ncu702.backend.service.impl;


import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ncu702.backend.common.ErrorCode;
import com.ncu702.backend.exception.BusinessException;
import com.ncu702.backend.mapper.ArticleMapper;
import com.ncu702.backend.mapper.CommentMapper;
import com.ncu702.backend.mapper.UserMapper;
import com.ncu702.backend.model.dto.articles.ArticleVo;
import com.ncu702.backend.model.entity.Article;
import com.ncu702.backend.model.entity.User;
import com.ncu702.backend.service.ArticleService;
import com.ncu702.backend.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Resource
    ArticleMapper articleMapper;
    @Resource
    UserMapper userMapper;


    /**
     * 根据id获取文章内容
     *
     * @param articleId
     */
    @Override
    public ArticleVo getArticleById(String articleId) {
        ArticleVo article = articleMapper.getArticleById(articleId);
        return article;
    }

    @Override
    public List<ArticleVo> getArticleByUid(long uid) {
        List<ArticleVo> articleVos = articleMapper.getArticleByUid(uid);
        return articleVos;
    }


    @Override
    public Integer deleteArticleById(String articleId) {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", articleId);
        return articleMapper.delete(wrapper);

    }

    /*
    根据id删除文章
     */

    /**
     * 获取主页数据
     *
     * @return
     */
    @Override
    public PageInfo<ArticleVo> getAllArticleHome(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleVo> articleVos = articleMapper.getAllArticleHome();
        return new PageInfo<>(articleVos);
    }

    /**
     * 主页数据根据按最新时间发布排序
     *
     * @return
     */
    @Override
    public PageInfo<ArticleVo> getAllArticleHomeToTime(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleVo> articleVos = articleMapper.getAllArticleHomeToTime();
        return new PageInfo<>(articleVos);
    }

    /**
     * 获取板块信息 根据热度排序desc
     *
     * @return
     */
    @Override
    public PageInfo<ArticleVo> getAllArticleBlock(Integer op, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleVo> articleVos = articleMapper.getAllArticleBlock(op);
        return new PageInfo<>(articleVos);
    }

    /**
     * 获取板块信息 根据发布时间排序desc
     *
     * @return
     */
    @Override
    public PageInfo<ArticleVo> getAllArticleBlockToTime(Integer op, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleVo> articleVos = articleMapper.getAllArticleBlockToTime(op);
        return new PageInfo<>(articleVos);
    }

    /**
     * 获取精华信息 根据热度排序desc
     *
     * @return
     */
    @Override
    public PageInfo<ArticleVo> getAllArticleElite(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleVo> articleVos = articleMapper.getAllArticleElite();
        return new PageInfo<>(articleVos);
    }

    /**
     * 获取精华信息 根据发布时间排序desc
     *
     * @return
     */
    @Override
    public PageInfo<ArticleVo> getAllArticleEliteToTime(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleVo> articleVos = articleMapper.getAllArticleEliteToTime();
        return new PageInfo<>(articleVos);
    }


    /**
     * 发布文章
     *
     * @param uid
     * @param title
     * @param label
     * @param summary
     * @param editorType
     * @param content
     * @param markdownContent
     */
    @Override
    public Integer addArticleByuid(Long uid,
                                   String title,
                                   Integer label,
                                   String summary,
                                   Integer editorType,
                                   String content,
                                   String markdownContent) {
        User userByUid = userMapper.getUserByUid(uid);
        if (userByUid == null) {
            throw new BusinessException(ErrorCode.FORBIDDEN_ERROR, "用户不存在");
        }
        if (userByUid.getIsDelete() == 0) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "用户已注销");
        }

        Article article = new Article();
        article.setArticleId(UUID.randomUUID().toString());
        article.setUid(uid);
        article.setUserName(userByUid.getUserName());
        article.setTitle(title);
        article.setLabel(label);
        article.setContent(content);
        article.setMarkdownContent(markdownContent);
        article.setEditorType(editorType);
        article.setSummary(summary);
        article.setPostTime(new Date());
        article.setUpdateTime(new Date());
        article.setReadCount(0);
        article.setGoodCount(0);
        article.setCommentCount(0);
        article.setStatus(1);
        article.setElite(0);
        article.setTop(0);
        article.setHelp(0);
        Integer integer = articleMapper.addArticleByuid(article);
        return integer;
    }


    /*
    update文章
     */
    @Override
    public Integer updateArticle(long uid,
                                 String articleId,
                                 String title,
                                 Integer label,
                                 String summary,
                                 String content,
                                 Integer editType,
                                 String markdownContext) {
        User userByUid = userMapper.getUserByUid(uid);
        if (userByUid == null) {
            throw new BusinessException(ErrorCode.FORBIDDEN_ERROR, "用户不存在");
        }
        if (userByUid.getIsDelete() == 0) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "用户已注销");
        }


        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("article_id", articleId);
        Article entity = new Article();
        entity.setUid(uid);
        entity.setTitle(title);
        entity.setLabel(label);
        entity.setSummary(summary);
        entity.setContent(content);
        entity.setEditorType(editType);
        entity.setMarkdownContent(markdownContext);
        entity.setPostTime(new Date());
        return articleMapper.update(entity, updateWrapper);
    }


    /*
    设置精华
     */
    @Override
    public Integer addElite(String articleId) {
        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("elite", 1).eq("article_id", articleId);
        return articleMapper.update(null, updateWrapper);
    }

    /*
   取消精华
    */
    @Override
    public Integer deleteElite(String articleId) {
        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("elite", 0).eq("article_id", articleId);
        return articleMapper.update(null, updateWrapper);
    }


    /*
    设置置顶
 */
    @Override
    public Integer addTop(String articleId) {
        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("top", 1).eq("article_id", articleId);
        return articleMapper.update(null, updateWrapper);
    }

    /*
   取消置顶
    */
    @Override
    public Integer deleteTop(String articleId) {
        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("top", 0).eq("article_id", articleId);
        return articleMapper.update(null, updateWrapper);
    }


    /**
     * 添加阅读量
     *
     * @param articleId
     */
    @Override
    public Integer updateReadCount(String articleId) {
        //先查询文章存不存在
        ArticleVo article = articleMapper.getArticleById(articleId);
        if (article == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "文章不存在");
        }
        if (article.getStatus() != 1) {
            throw new BusinessException(ErrorCode.FORBIDDEN_ERROR, "文章没被审核");
        }

        Integer readCount = article.getReadCount();
        Integer integer = articleMapper.updateReadCount(++readCount, articleId);
        return integer;
    }


    /*
    删除文章
     */


    /**
     * 获取文章作者的图片
     *
     * @param uid
     */
    @Override
    public User getInfo(Integer uid) {
        User userByUid = userMapper.getUserByUid(uid);
        return userByUid;
    }

}
